package com.filldream.fastboot.core.event.listener;

import com.filldream.fastboot.core.service.ScheduleJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.StringUtils;

/**
 * @author RickSun
 * @Description 项目启动日志
 **/
@Slf4j
@Configuration
public class StartEventListener {

//	@Async
//	@Order
//	@EventListener(WebServerInitializedEvent.class)
//	public void afterStart(WebServerInitializedEvent event) {
//		Environment environment = event.getApplicationContext().getEnvironment();
//		String appName = environment.getProperty("project.name");
//		String path = environment.getProperty("server.servlet.context-path");
//		if(path == null ) { path = ""; }
//		int localPort = event.getWebServer().getPort();
//		String profile = StringUtils.arrayToCommaDelimitedString(environment.getActiveProfiles());
//		log.info("---WebSocket日志地址:{}---", "ws://IP:" + localPort + "/websocket/logging/{token}"  );
//		log.info("---[{}]启动完成,端口:[{}],环境变量:[{}],接口文档:[{}],上下文路径:[{}]---", appName, localPort, profile, "IP或域名:"+localPort +path + "/doc.html",path);
//	}

	@Autowired
	private ScheduleJobService jobService;

	@Bean
	public ApplicationRunner runner(WebServerApplicationContext context) {
		return args -> {
			Environment environment = context.getEnvironment();
			String appName = environment.getProperty("project.name");
			String path = environment.getProperty("server.servlet.context-path");
			if(path == null ) { path = ""; }
			int localPort = context.getWebServer().getPort();
			String profile = StringUtils.arrayToCommaDelimitedString(environment.getActiveProfiles());

			log.info("-----重启定时任务中-----"  );
			jobService.init(profile.equals("local") ? "JianLaiTask" : "dd");

//			log.info("---IP黑名单同步中---");
//			jobService.redisToLocalCache();

			log.info("---WebSocket日志地址:{}---", "ws://IP:" + localPort + path + "/websocket/logging/{token}"  );
			log.info("---[{}]启动完成,端口:[{}],环境变量:[{}],接口文档:[{}],上下文路径:[{}]---", appName, localPort, profile, "IP或域名:"+localPort +path + "/doc.html",path);
		};
	}

}
